package net.lab1024.sa.admin.module.business.erp.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.dao.AccountDao;
import net.lab1024.sa.admin.module.business.erp.dao.WageDao;
import net.lab1024.sa.admin.module.business.erp.dao.WageaDao;
import net.lab1024.sa.admin.module.business.erp.domain.entity.AccountEntity;
import net.lab1024.sa.admin.module.business.erp.domain.entity.WageEntity;
import net.lab1024.sa.admin.module.business.erp.domain.entity.WageaEntity;
import net.lab1024.sa.admin.module.business.erp.domain.form.add.WageaAddForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.query.WageaQueryForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.update.WageaUpdateForm;
import net.lab1024.sa.admin.module.business.erp.domain.vo.WageaVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 工资明细 Service
 *
 * @Author AKITA
 * @Date 2025-04-19 13:25:43
 * @Copyright AKITA
 */

@Service
public class WageaService {

    @Resource
    private WageaDao wageaDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private WageDao wageDao;

    public PageResult<WageaVO> queryPage(WageaQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<WageaVO> list = wageaDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public ResponseDTO<String> add(WageaAddForm addForm) {
        WageaEntity wageaEntity = SmartBeanUtil.copy(addForm, WageaEntity.class);
        wageaDao.insert(wageaEntity);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> update(WageaUpdateForm updateForm) {
        WageaEntity wageaEntity = SmartBeanUtil.copy(updateForm, WageaEntity.class);
        BigDecimal add = wageaEntity.getBasicWage().add(wageaEntity.getPostWage()).add(wageaEntity.getMeritsWage()).add(wageaEntity.getDanPostWage()).
                add(wageaEntity.getMealWage()).add(wageaEntity.getOverTimeWage()).add(wageaEntity.getPhoneWage()).add(wageaEntity.getAgeWage()).
                add(wageaEntity.getReissueWage()).add(wageaEntity.getOtherWage()).add(wageaEntity.getOther());
        wageaEntity.setPayableWage(add);
        BigDecimal add1 = wageaEntity.getPensionSafe().add(wageaEntity.getHealthSafe()).add(wageaEntity.getLoseSafe()).add(wageaEntity.getTaxableAmount()).
                add(wageaEntity.getPersIncoTax()).add(wageaEntity.getRebateTax()).add(wageaEntity.getDeduWage());
        BigDecimal subtract = add.subtract(add1);
        wageaEntity.setPayableAmount(subtract);
        wageaDao.update(wageaEntity,new LambdaQueryWrapper<WageaEntity>().eq(WageaEntity::getDocEntry,wageaEntity.getDocEntry()).eq(WageaEntity::getLineNum,wageaEntity.getLineNum()));
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        wageaDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        WageEntity wageEntity = wageDao.selectById(docEntry);
        List<AccountEntity> accountEntities = accountDao.selectList(new LambdaQueryWrapper<AccountEntity>().eq(AccountEntity::getAbsId, wageEntity.getAbsId()));
        if (accountEntities != null) {
            for (AccountEntity accountEntity : accountEntities) {
                accountEntity.setDocStatus("O");
                accountDao.updateById(accountEntity);
            }

        }
        wageaDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }


    @Transactional
    public ResponseDTO<String> deleteLine(WageaAddForm addForm) {
        wageaDao.delete(new LambdaQueryWrapper<WageaEntity>().eq(WageaEntity::getDocEntry,addForm.getDocEntry()).eq(WageaEntity::getLineNum,addForm.getLineNum()));
        return ResponseDTO.ok();
    }
}
